# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

if (!exists("AllScriptFlag", 1)) {
  source("./Scripts/1Header.R")
}

#### Table 1: Summary Statistics
Main       <- read_dta("Data/Main.dta")
Coviraites <- read_dta("Data/Coviraites.dta")
Main       <- left_join(Main, Coviraites, by = c("ID", "Date"))

CrossSection <- read_dta("Data/CrossSection.dta") %>% filter(ForMatchFlag == 1)
Table1_Plain <- SummaryStatistics(Data = Main, CrossSection = CrossSection)

Label1_ColName <- c("Full Sample", "Cross-section (at start of panel)", "Propensity-matched sample")
Label2_ColName <- c("Obs.",	"Mean",	"Median",	"Std. Dev.",
                    "Mean", "Mean",	"Diff",	"p value",
                    "Mean", "Mean",	"Diff",	"p value",	"\\% balance ")
Label3_ColName <- "improved"
Label4_ColName <- c("Affected", "Unaffected")
Label5_ColName <- c("N=144", "N=233")
Label6_ColName <- c("N=141", "N=141")

## Produce latex table 
Table1 <- 
    TR("") %:% TR(seq(from = 1, to = 13, by = 1), se = TRUE, dec = 0) + 
  midrulep(list(c(2, 14))) +
    TR("") %:% TR(Label1_ColName, cspan = c(4, 4, 5)) +
  midrulep(list(c(2, 5), c(6, 9), c(10, 14))) +
    TR("") %:% TR(Label2_ColName) +
    TR("") %:% TR(rep("", 12)) %:% TR(Label3_ColName) +
  midrulep(list(c(2, 14))) +
    TR("") %:% TR(rep("", 4)) %:% TR(Label4_ColName) %:% TR(rep("", 2)) %:% TR(Label4_ColName) %:% TR(rep("", 3)) +
    TR("") %:% TR(rep("", 4)) %:% TR(Label5_ColName) %:% TR(rep("", 2)) %:% TR(Label6_ColName) %:% TR(rep("", 3)) +
  midrule() +
    TR(Table1_Plain$RowNames[ 1]) %:% TR(Table1_Plain$Basic[,  1], dec = c(0, rep(2, 3))) %:% TR(Table1_Plain$BalanceTest[,  1], dec = 2) + 
    TR(Table1_Plain$RowNames[ 2]) %:% TR(Table1_Plain$Basic[,  2], dec = c(0, rep(2, 3))) %:% TR(Table1_Plain$BalanceTest[,  2], dec = 2) + 
    TR(Table1_Plain$RowNames[ 3]) %:% TR(Table1_Plain$Basic[,  3], dec = c(0, rep(2, 3))) %:% TR(Table1_Plain$BalanceTest[,  3], dec = 2) + 
  midrule() +
    TR(Table1_Plain$RowNames[ 4]) %:% TR(Table1_Plain$Basic[,  4], dec =      rep(0, 4))  %:% TR(Table1_Plain$BalanceTest[,  4], dec = c(rep(0, 2), rep(2, 2), rep(0, 3), rep(2, 2))) + 
    TR(Table1_Plain$RowNames[ 5]) %:% TR(Table1_Plain$Basic[,  5], dec = c(0, rep(2, 3))) %:% TR(Table1_Plain$BalanceTest[,  5], dec = 2) + 
    TR(Table1_Plain$RowNames[ 8]) %:% TR(Table1_Plain$Basic[,  8], dec = c(0, rep(2, 3))) %:% TR(Table1_Plain$BalanceTest[,  8], dec = 2) + 
    TR(Table1_Plain$RowNames[ 9]) %:% TR(Table1_Plain$Basic[,  9], dec = c(0, rep(2, 3))) %:% TR(Table1_Plain$BalanceTest[,  9], dec = 2)

## Output files (.tex and .pdf)
TS(Table1, file = "Table1",
   pretty_rules = TRUE, header = c('l',rep('c',13)),
   output_path = paste0(getwd(), "/Output"))

## Open Table1.pdf with default PDF viewer
system2('open', args = 'Output/Table1.pdf', wait = FALSE)

## Remove intermediate products
rm(list = grep(pattern = "Main|Coviraites|CrossSection|Label", ls(), value = TRUE))
